<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="姓名:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item>
        <el-form-item label="电话:">
          <el-input v-model="formInline.phone" clearable />
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="formInline.disable" placeholder="请选择" clearable>
            <el-option label="在职" value="0" />
            <el-option label="离职" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button icon="el-icon-arrow-left" @click="cancel">
            返 回
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-edit" :disabled="disabled" @click="edit">
            编辑
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-user-solid" :disabled="disabled" @click="enable('0')">
            设置在职
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-user" :disabled="disabled" @click="enable('1')">
            设置离职
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-set-up" :disabled="disabled" @click="changePassword">
            初始化密码
          </el-button>
        </el-form-item>
      </el-form>

      <div class="result" style="margin-top: -10px;">
        <div class="table">
          <el-table ref="multipleTable" v-loading="loading" :data="tableData" border tooltip-effect="dark"
                    style="width: 100%" @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="virtualCard" label="虚拟卡号" />
            <el-table-column label="性别" prop="sex" width="120">
                  <template slot-scope="scope">
                    <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.sexColor">{{
              scope.row.sex
            }}</el-tag>
                  </template>
                </el-table-column>
            
                <el-table-column label="微信公众平台" prop="weChatOpenId" width="120">
                  <template slot-scope="scope">
                    <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.weChatColor">{{
              scope.row.weChat
            }}</el-tag>
                  </template>
                </el-table-column>
            
            
                <el-table-column label="状态" prop="disabled" width="120">
                  <template slot-scope="scope">
                    <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.disabledColor">{{
              scope.row.disabled
            }}</el-tag>
                  </template>
                </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
              <template slot-scope="scope">
                {{ scope.row.createTime | parseDate }}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next, sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../mixins/index";
import {
  userDisalbe
} from "../../utils/resource.js";
import {
  getUsers,
  enable,
  changePassword
} from "../../api/user.js";
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
    parseSex(value) {
      if (value === "1") {
        return "女";
      } else {
        return "男";
      }
    },
    parseWeChat(value) {
      if (value === null) {
        return "未绑定";
      } else {
        return "已绑定";
      }
    },
    parseUserDisable(value) {
      if (value != null) {
        return userDisalbe.find((p) => p.value === `${value}`).label;
      }
    },
  },
  mixins: [pageMixin],
  props: {
    orgId: {
      type: String,
      default: ""
    },
    parentType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      formInline: {},
    };
  },
  computed: {},
  async created() {

    await this.init();
  },
  methods: {
    async init() {
      this.tableData = [];
      this.loading = true;
      const {
        data,
        count
      } = await getUsers({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          Name: this.formInline.name,
          Phone: this.formInline.phone,
          Disabled: this.formInline.disable,
          OrgGuid: this.orgId,
          Status: 10,
        }),
      });
      this.tableData = data;
      this.tableData.forEach(item => {
        if (item.sex === '1') {
          item.sex = "女";
          item.sexColor = "red";
        } else {
          item.sex = "男";
          item.sexColor = 'blue';
        }

        if (item.weChatOpenId === '0') {
          item.weChat = "未绑定";
          item.weChatColor = "red";
        } else {
          item.weChat = "已绑定";
          item.weChatColor = 'blue';
        }
        var obj2 = userDisalbe.find((p) => p.value === `${item.disabled.toString()}`);
        if (obj2) {
          item.disabled = obj2.label;
          item.disabledColor = obj2.color;
        } else {
          item.disabled = "无数据";
          item.disabledColor = "red";
        }
      })
      this.total = count;
      this.loading = false;
    },
    cancel() {
      switch (this.parentType) {
        case "10":
          this.$router.push("/organization/accept");
          break;
        case "20": // 产废单位
          this.$router.push("/organization/wasteproduction");
          break;
        case "30": // 处置单位
          this.$router.push("/organization/handle");
          break;
        case "40": // 运输单位
          this.$router.push("/organization/transport");
          break;
          case "50": // 运输单位
            this.$router.push("/organization/supervise");
            break;
      }
    },
    changePassword() {
      const arr = this.$refs.multipleTable.selection;
      if (arr.length < 1) {
        this.$message.error("请至少选择一条数据");
        return;
      }

      this.$confirm(`此操作将初始化所选用户登录密码, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
        let ids2 = "";
        for (let i = 0; i < arr.length; i++) {
          if (i === 0) {
            ids2 += arr[i].guid;
          } else {
            ids2 += "," + arr[i].guid;
          }
        }
        var formdata = {
          Guid: ids2,
        };

        // 操作数据
        await changePassword(formdata);
        await this.init();
        this.$message({
          type: "success",
          message: "初始化密码成功",
        });
      });
    },
    async enable(value) {
      const arr = this.$refs.multipleTable.selection;
      if (arr.length !== 1) {
        this.$message.error("此功能仅限操作1条数据！");
        return;
      }

      let confimText = "";
      if (value === "0") {
        confimText = "在职";
      } else {
        confimText = "离职";
      }
      this.$confirm(
        `此操作将所选用户信息状态设置为${confimText}状态, 是否继续?`,
        "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(async() => {
        let ids2 = "";
        for (let i = 0; i < arr.length; i++) {
          if (i === 0) {
            ids2 += arr[i].guid;
          } else {
            ids2 += "," + arr[i].guid;
          }
        }

        var formdata = {
          Guid: ids2,
          Disabled: Number(value),
        };

        // 操作数据
        await enable(formdata);
        await this.init();
        this.$message({
          type: "success",
          message: `${confimText}状态更新成功!`,
        });
      });
    },
    edit() {
      const arr = this.$refs.multipleTable.selection;

      this.$emit("edit", arr[0].guid);
    },
    add() {
      return this.$emit("add", this.orgId);
    },
    async reset() {
      this.formInline = {
        name: "",
        phone: "",
        disable: "",
      };
      await this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
